//
//  QNRollView.h
//  QNReuseTool
//
//  Created by rsh on 2020/7/15.
//  Copyright © 2020年 rsh. All rights reserved.
//

// MARK: 跑马灯效果

#import <UIKit/UIKit.h>

/**
 默认cell样式 QNItemID
 */
@interface QNRollViewCell : UICollectionViewCell
@end

@class QNRollView;

//代理协议
@protocol QNRollViewDelegate <NSObject>
@optional
/**
 返回itemSize 默认值是CGSizeMake(self.frame.size.width, self.frame.size.height);
 */
- (CGSize)rollView:(QNRollView *)rollView sizeForItemAtIndex:(NSInteger)index;
/**
 item的间隔 默认值0
 */
- (CGFloat)spaceOfItemInRollView:(QNRollView *)rollView;
/**
 内边距 上 左 下 右 默认值UIEdgeInsetsMake(0, 0, 0, 0)
 */
- (UIEdgeInsets)paddingOfRollView:(QNRollView *)rollView;
/**
 点击事件
 */
- (void)rollView:(QNRollView *)rollView didSelectItemAtIndex:(NSInteger)index;
/**
 翻页完成的回调 只针对于分页效果
 */
- (void)rollView:(QNRollView *)rollView didRollItemToIndex:(NSInteger)currentIndex;
/**
 自定义item样式
 */
- (QNRollViewCell *)rollView:(QNRollView *)rollView cellForItemAtIndex:(NSInteger )index;
@end

/**
 滚动样式
 */
typedef NS_ENUM(NSInteger, QNRollViewScrollStyle) {
    QNRollViewScrollStylePage = 0, /** 分页 必须等宽或高*/
    QNRollViewScrollStyleStep   /** 渐进 可以不等宽或高*/
};

@interface QNRollView : UIView

/**
 原始数据源
 */
@property (nonatomic, strong) NSMutableArray * sourceArray;

/**
 是否循环轮播 默认YES 如果NO，则自动禁止计时器
 */
@property (nonatomic, assign) BOOL loopEnabled;
/**
 是否允许滑动 默认YES
 */
@property (nonatomic, assign) BOOL scrollEnabled;
/**
 轮播方向 默认是 UICollectionViewScrollDirectionHorizontal 水平
 */
@property (nonatomic, assign) UICollectionViewScrollDirection scrollDirection;
/**
 轮播样式 默认是 QNRollViewScrollStylePage 分页
 */
@property (nonatomic, assign) QNRollViewScrollStyle scrollStyle;
/**
 设置初始化时的位置页码，默认为0 只对分页效果有效
 */
@property (nonatomic, assign) NSInteger startingPosition;

/**
 渐进轮播速率 单位是Point/s，以坐标系单位为准 默认60/s 如果为0 表示禁止计时器
 */
@property (nonatomic, assign) CGFloat speed;
/**
 分页轮播间隔时长 单位是s  默认3s 如果为0 表示禁止计时器
 */
@property (nonatomic, assign) CGFloat interval;

/**
 item的间隔 默认值0
 */
@property (nonatomic, assign) CGFloat spaceOfItem;

/**
 内边距 上 左 下 右 默认值UIEdgeInsetsMake(0, 0, 0, 0)
 */
@property (nonatomic, assign) UIEdgeInsets padding;

/** delegate*/
@property (nonatomic, weak) id<QNRollViewDelegate> delegate;

/**
 初始化方法 direction 滚动方向
 */
- (instancetype)initWithFrame:(CGRect)frame scrollDirection:(UICollectionViewScrollDirection)direction;

/**
 注册item样式 用法和UICollectionView相似
 */
- (void)registerClass:(nullable Class)cellClass forCellWithReuseIdentifier:(NSString *)identifier;
/**
 注册item样式 用法和UICollectionView相似
 */
- (void)registerNib:(nullable UINib *)nib forCellWithReuseIdentifier:(NSString *)identifier;
/**
 用于初始化QNRollViewCell，自定义cell样式 用法和UICollectionView相似
 */
- (QNRollViewCell *)dequeueReusableCellWithReuseIdentifier:(NSString *)identifier forIndex:(NSInteger)index;
//返回索引为index的cell
- (QNRollViewCell *)cellForItemAtIndexPath:(NSInteger)index;

/**
 刷新数据源
 */
- (void)reloadData;
/**
 暂停自动轮播
 */
- (void)pause;
/**
 继续自动轮播
 */
- (void)play;

@end



/**
 *用法简单介绍： ----- 新建一个自定义UIView
 
 
 #import <UIKit/UIKit.h>
 #import "GBBusinessNoticeModel.h"

 NS_ASSUME_NONNULL_BEGIN

 // MARK: 首页商家公告View

 @interface GBBusinessNoticeView : UIView

 // MARK: 更多按钮点击block
 @property(copy, nonatomic)void (^moreBtnActionBlock)(void);
 // MARK: cell点击block
 @property(copy, nonatomic)void (^didSelectItemBlock)(GBBusinessNoticeModel *model);
 // MARK: 数据源
 @property (nonatomic, strong) NSMutableArray *dataSource;

 @end
 
 
 
 #import "GBBusinessNoticeView.h"
 #import "QNRollView.h"

 // MARK: 首页商家公告View


 @interface QNRollViewVerticalCell : QNRollViewCell
 @property (strong, nonatomic) UILabel * titleLabel;
 @end

 @implementation QNRollViewVerticalCell

 - (instancetype)initWithFrame:(CGRect)frame{
     if (self == [super initWithFrame:frame]) {
         self.titleLabel = [[UILabel alloc] init];
         self.titleLabel.textAlignment = NSTextAlignmentLeft;
         self.titleLabel.font = [UIFont systemFontOfSize:13];
         self.titleLabel.textColor = ASColorHex(0x222222);
         self.titleLabel.userInteractionEnabled = YES;
         [self.contentView addSubview:self.titleLabel];
         self.contentView.clipsToBounds = YES;
     }
     return self;
 }
 - (void)refreshData{
     self.titleLabel.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
 }
 @end



 @interface GBBusinessNoticeView()<QNRollViewDelegate>

 @property (nonatomic, strong) QNRollView *rollview;

 @end

 @implementation GBBusinessNoticeView

 - (instancetype)initWithFrame:(CGRect)frame {
     if (self = [super initWithFrame:frame]) {
         self.backgroundColor = [UIColor whiteColor];
         [self setUI];
     }
     return self;
 }

 -(void)setUI{
     //轮廓
     UIView *view1 = [[UIView alloc] init];
     view1.backgroundColor = UIColor.whiteColor;
     view1.layer.borderColor = ASColorHexAlpha(0x000000,0.2).CGColor;
     view1.layer.borderWidth = 1.0f;
     view1.layer.masksToBounds = YES;
     view1.layer.cornerRadius = 9.0f;
     [self addSubview:view1];
     [view1 mas_makeConstraints:^(MASConstraintMaker *make) {
         make.left.mas_equalTo(10);
         make.right.mas_equalTo(-10);
         make.top.bottom.mas_equalTo(0);
     }];
     
     UILabel *label = UILabel.alloc.init;
     label.text = @"商家公告";
     label.textColor = ASColorHex(0x101010);
     label.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:13];
     NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:label.text];
     [attri addAttribute:NSForegroundColorAttributeName value:ASColorHex(0xFF1A40) range:NSMakeRange(0, 2)];
     label.attributedText = attri;
     [self addSubview:label];
     [label mas_makeConstraints:^(MASConstraintMaker *make) {
         make.left.mas_equalTo(view1.mas_left).offset(10);
         make.centerY.mas_equalTo(0);
     }];
     
     UIView *view2 = [[UIView alloc] init];
     view2.backgroundColor = ASColorHexAlpha(0x000000,0.2);
     [self addSubview:view2];
     [view2 mas_makeConstraints:^(MASConstraintMaker *make) {
         make.left.mas_equalTo(view1.mas_left).offset(72);
         make.centerY.mas_equalTo(0);
         make.height.mas_equalTo(15);
         make.width.mas_equalTo(1);
     }];
     
     UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
     btn.backgroundColor = ASColorHexAlpha(0x000000,0.05);
     btn.layer.cornerRadius = 5.0f;
     btn.layer.masksToBounds = YES;
     [btn setTitle:@"更多" forState:UIControlStateNormal];
     [btn setTitleColor:ASColorHex(0x000000) forState:UIControlStateNormal];
     btn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:11];
     [btn addTarget:self action:@selector(moreBtnClick) forControlEvents:UIControlEventTouchUpInside];
     [self addSubview:btn];
     [btn mas_makeConstraints:^(MASConstraintMaker *make) {
         make.right.mas_equalTo(view1).offset(-10);
         make.centerY.mas_equalTo(0);
         make.width.mas_equalTo(45);
         make.height.mas_equalTo(20);
     }];
     
     [self.rollview registerClass:[QNRollViewVerticalCell class] forCellWithReuseIdentifier:@"PageRollID"];
     [self addSubview:self.rollview];
 }


 -(void)setDataSource:(NSMutableArray *)dataSource{
     _dataSource = dataSource;
     self.rollview.sourceArray = self.dataSource;
     [self.rollview reloadData];
 }


 #pragma mark - QNRollViewDelegate

 - (void)rollView:(QNRollView *)rollView didSelectItemAtIndex:(NSInteger)index{
     NSLog(@" 点击了 %ld",index);
     if (self.didSelectItemBlock) {
         GBBusinessNoticeModel *model = self.dataSource[index];
         self.didSelectItemBlock(model);
     }
 }


 //返回自定义cell样式
 -(QNRollViewCell *)rollView:(QNRollView *)rollView cellForItemAtIndex:(NSInteger)index{
     
     GBBusinessNoticeModel *model = self.dataSource[index];
     QNRollViewVerticalCell * cell = (QNRollViewVerticalCell *)[rollView dequeueReusableCellWithReuseIdentifier:@"PageRollID" forIndex:index];
     cell.titleLabel.text = model.title;
     [cell refreshData];
     return cell;
 }


 // MARK: 更多
 -(void)moreBtnClick{
     if (self.moreBtnActionBlock) {
         self.moreBtnActionBlock();
     }
 }

 -(QNRollView *)rollview{
     if (!_rollview) {
         _rollview = [[QNRollView alloc] initWithFrame:CGRectMake(92, 2, ASScreenWidth - (92+85), 36)];
         _rollview.backgroundColor = [UIColor whiteColor];
         _rollview.scrollStyle = QNRollViewScrollStylePage;
         _rollview.scrollDirection = UICollectionViewScrollDirectionVertical;
         _rollview.interval = 2.5;
         _rollview.delegate = self;
     }
     return _rollview;
 }


 @end

 
 
 
 
 
 
 ////////**** 可把自定义的 view 添加到 UICollectionViewCell 上
 
 
 #import <UIKit/UIKit.h>
 #import "GBBusinessNoticeView.h"

 NS_ASSUME_NONNULL_BEGIN

 @interface GB_NotificationCell : UICollectionViewCell

 @property(copy, nonatomic)void (^moreBtnActionBlock)(void);
 @property(copy, nonatomic)void (^didSelectItemBlock)(GBBusinessNoticeModel *model);
 @property (nonatomic, strong) GBBusinessNoticeView *businessNoticeView;

 @end
 
 
 #import "GB_NotificationCell.h"

 @implementation GB_NotificationCell

 - (instancetype)initWithFrame:(CGRect)frame
 {
     self = [super initWithFrame:frame];
     if (self) {
         self.contentView.backgroundColor = UIColor.whiteColor;
         [self.contentView addSubview:self.businessNoticeView];
         [self.businessNoticeView mas_makeConstraints:^(MASConstraintMaker *make) {
 //            make.edges.mas_equalTo(0);
             make.left.right.mas_equalTo(0);
             make.top.mas_equalTo(5);
             make.bottom.mas_equalTo(-5);
         }];
         ASWeak(self);
         self.businessNoticeView.moreBtnActionBlock = ^{
             if (weakself.moreBtnActionBlock) {
                 weakself.moreBtnActionBlock();
             }
         };
         self.businessNoticeView.didSelectItemBlock = ^(GBBusinessNoticeModel * _Nonnull model) {
             if (weakself.didSelectItemBlock) {
                 weakself.didSelectItemBlock(model);
             }
         };
     }
     return self;
 }


 - (GBBusinessNoticeView *)businessNoticeView{
     if (!_businessNoticeView) {
         _businessNoticeView = GBBusinessNoticeView.alloc.init;
     }
     return _businessNoticeView;
 }

 @end
 
 
 
 
 
 ////////********* 最后在外层控制器中 应用 UICollectionViewCell
 
 GB_NotificationCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:GB_NotificationCell.description forIndexPath:indexPath];
 cell.businessNoticeView.dataSource = [NSMutableArray arrayWithArray:viewModel.messagesContent];
 ASWeak(self);
 cell.moreBtnActionBlock = ^{
     // 更多
 };
 cell.didSelectItemBlock = ^(GBBusinessNoticeModel * _Nonnull model) {
     // didSelectItem
     [weakself jumpToControllerWithFlag:7 parameters:model.iosLinkUrl];
 };
 return cell;
 


 *
 *
 *
 *
 **/
